package com.jss.lanouhn.fristapp.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.google.gson.Gson;
import com.hyg.dropdownmenu.DropDownMenu;
import com.hyg.dropdownmenu.DropDownMenuUtils;
import com.jss.lanouhn.fristapp.R;
import com.jss.lanouhn.fristapp.adapters.SearchAdapter;
import com.jss.lanouhn.fristapp.modles.SearchContent;
import com.jss.lanouhn.fristapp.modles.SearchNames;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class SearchContentActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private SearchNames names;
    private SearchContent content;
    private SearchAdapter adapter;

    private DropDownMenu mDropDownMenu;
    private DropDownMenu mDropDownMenu1;
    List<String> target = new ArrayList<>();
    List<String> scene = new ArrayList<>();
    List<String> personality = new ArrayList<>();
    List<String> price = new ArrayList<>();

    List<Integer> targetId = new ArrayList<>();
    List<Integer> sceneId = new ArrayList<>();
    List<Integer> personalityId = new ArrayList<>();
    List<String> priceId = new ArrayList<>();

    private String url = "http://api.liwushuo.com/v2/search/item_filter";
    //页容量
    private int limit = 20;
    //页码
    private int offset = 0;
    private String priceIdd = "";
    private String personalityIdd = "";
    private String targetIdd = "";
    private String sceneIdd = "";
    private String sort="";
    private String urlStr = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_content);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.rv_act_search);
        recyclerView.setLayoutManager(new GridLayoutManager(SearchContentActivity.this, 2));
        mDropDownMenu = (DropDownMenu) findViewById(R.id.dropDownMenu);
        mDropDownMenu1 = (DropDownMenu) findViewById(R.id.dropDownMenu1);
    }

    private void initData() {
        //解析下拉菜单数据
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(url)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        Gson gson = new Gson();
                        names = gson.fromJson(result, SearchNames.class);
                        handler.sendEmptyMessage(2);
                    }
                });
            }
        }).start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 2) {
                List<SearchNames.DataBean.FiltersBean> list = names.getData().getFilters();
                String[] tabNames = new String[]{"对象", "场合", "个性", "价格"};
                target.add("全部");
                personality.add("全部");
                scene.add("全部");
                price.add("全部");
                for (int j = 0; j < list.size(); j++) {
                    switch (list.get(j).getId()) {
                        case 1:
                            //对象
                            for (SearchNames.DataBean.FiltersBean.ChannelsBean bean : list.get(j).getChannels()) {
                                target.add(bean.getName());
                                targetId.add(bean.getId());
                            }
                            break;
                        case 2:
                            //个性
                            for (SearchNames.DataBean.FiltersBean.ChannelsBean bean : list.get(j).getChannels()) {
                                personality.add(bean.getName());
                                personalityId.add(bean.getId());
                            }
                            break;
                        case 3:
                            //场合
                            for (SearchNames.DataBean.FiltersBean.ChannelsBean bean : list.get(j).getChannels()) {
                                scene.add(bean.getName());
                                sceneId.add(bean.getId());
                            }
                            break;
                        case 4:
                            //价格
                            for (SearchNames.DataBean.FiltersBean.ChannelsBean bean : list.get(j).getChannels()) {
                                price.add(bean.getName());
                                priceId.add(bean.getKey() + "");
                            }
                            break;
                    }
                }
                //设置默认名称
                mDropDownMenu1.setTabNames(tabNames);
                //给下拉菜单绑定数据
                DropDownMenuUtils.addGridList(SearchContentActivity.this, mDropDownMenu1, new DropDownMenuUtils.OnMenuClickListener() {
                    @Override
                    public void onMenuClick(int viewPosition, int itemPosition) {
                        if (itemPosition == 0) {
                            targetIdd = "";
                            sceneIdd = "";
                            personalityIdd = "";
                            priceIdd = "";
                        } else {
                            switch (viewPosition) {
                                case 0:
                                    targetIdd = targetId.get(itemPosition - 1) + "";
                                    break;
                                case 1:
                                    sceneIdd = sceneId.get(itemPosition - 1) + "";
                                    break;
                                case 2:
                                    personalityIdd = personalityId.get(itemPosition - 1) + "";
                                    break;
                                case 3:
                                    priceIdd = priceId.get(itemPosition - 1);
                                    break;
                            }
                        }
                        initEvent();
                    }
                }, target, scene, personality, price);

                DropDownMenuUtils.addTextList(SearchContentActivity.this, mDropDownMenu, new DropDownMenuUtils.OnMenuClickListener() {
                    @Override
                    public void onMenuClick(int viewPosition, int itemPosition) {
                        switch (itemPosition){
                            case 0:
                                sort="";
                                break;
                            case 1:
                                sort="hot";
                                break;
                            case 2:
                                sort="price:asc";
                                break;
                            case 3:
                                sort="price:desc";
                                break;
                        }
                        initEvent();
                    }
                }, Arrays.asList("默认排序", "按热度排序", "价格由低到高", "价格由高到低"));
            }
        }
    };


    private void initEvent() {
        //解析RecyclerView的数据
        new Thread(new Runnable() {
            @Override
            public void run() {
                urlStr = "http://api.liwushuo.com/v2/search/item_by_type?limit=" + limit + "&offset=" + offset +
                        "&price=" + priceIdd + "&personality=" + personalityIdd + "&target=" + targetIdd + "&scene=" + sceneIdd
                +"&sort="+sort;

                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(urlStr)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        Gson gson = new Gson();
                        content = gson.fromJson(result, SearchContent.class);
                        handler1.sendEmptyMessage(1);
                    }
                });
            }
        }).start();
    }

    private Handler handler1 = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                adapter = new SearchAdapter(SearchContentActivity.this, content);
                recyclerView.setAdapter(adapter);
            }
        }
    };

    @Override
    public void onBackPressed() {
        //退出activity前关闭菜单
        if (mDropDownMenu1.isShowing()) {
            mDropDownMenu1.closeMenu();
        } else {
            super.onBackPressed();
        }
    }
}
